import http from '@ohos.net.http';
import featureAbility from '@ohos.ability.featureAbility'
import preferences from '@ohos.data.preferences';

// 获取context并调用相关方法
let context = featureAbility.getContext();
//轻量级数据库
let storagePromise = preferences.getPreferences(context, 'mystore');

export function changePreState(ID,State,callback){
    var token;
    storagePromise.then((preferences) => {
        let getPromise = preferences.get('token', 'default');
        getPromise.then((value) => {
            console.info("CHWxxx---The value of 'startup' is " + value);
            token = value;
            let httpRequest = http.createHttp();
            httpRequest.request(
                "https://www.xdsbty.cn/admin/editprescription",
                {
                    method:'POST',
                    header: {
                        'Content-Type': 'application/json',
                        'Token':token
                    },
                    extraData: {
                        id: ID,
                        state: State
                    },
                    readTimeout: 10000,
                    connectTimeout: 10000
                }, (err, data) => {
                if (!err) {
                    console.info("utils xxx--- "+data.result )
                    callback(data.result)
                } else {
                    console.info('utils xxx--- error:' + JSON.stringify(err));
                    callback("error")
                }
            })
        }).catch((err) => {
            console.info("CHWxxx---Failed to get the value of 'startup'. Cause: " + err);
        })
    }).catch((err) => {
        console.info("CHWxxx---Failed to get preferences.")
    });


}